package com.example.product.statemachine;


import org.springframework.context.event.EventListener;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.statemachine.event.StateMachineEvent;
import org.springframework.stereotype.Component;

@Component
@WithStateMachine(name = "orderStateMachine")
public class OrderStatusListener {

    @OnTransition(source = "WAIT_PAYMENT", target = "WAIT_DELIVER")
    public boolean onEventDeliver(Message<OrderStatusChangeEventEnum> message) {
        Order order = message.getHeaders().get("order", Order.class);
        System.out.println("onEventDeliver.onEvent: " + message);
        System.out.println("onEventDeliver.order: " + order);
        order.setOrderStatus(OrderStatusEnum.WAIT_DELIVER);
        //订单表保存数据
        //发送站内消息
        //回调支付接口
        //同步至数据仓库
        return true;
    }

    @OnTransition(source = "WAIT_DELIVER", target = "WAIT_RECEIVE")
    public boolean onEventReceiver(Message<OrderStatusChangeEventEnum> message) {
        System.out.println("onEventReceiver.onEvent: " + message);
        Order order = message.getHeaders().get("order", Order.class);
        System.out.println("onEventReceiver.order: " + order);
        order.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE);
        return true;
    }

    @OnTransition(source = "WAIT_RECEIVE", target = "FINISH")
    public boolean onEventFinish(Message<OrderStatusChangeEventEnum> message) {
        System.out.println("onEventFinish.onEvent: " + message);
        Order order = message.getHeaders().get("order", Order.class);
        System.out.println("onEventFinish.order: " + order);
        order.setOrderStatus(OrderStatusEnum.FINISH);
        return true;
    }


}
